﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_News_ctrNewsDetails : System.Web.UI.UserControl
{
    private int nItenView = 0;
    private int nPageCount = 0;
    private string sTitle = string.Empty ;
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            this.ShareLink();
            this.LoadData();
        }
    }
    private void ShareLink()
    {
        hplFaceBook.NavigateUrl = "https://www.facebook.com/sharer/sharer.php?u=" + System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        hplGoogle.NavigateUrl = "https://www.google.com/bookmarks/mark?op=edit&amp;bkmk=" + System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        hplTwitter.NavigateUrl = "http://twitter.com/intent/session?original_referer=" + System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
    }
    private void LoadData()
    {
        try
        {
            int nCurrentLang = Support.GetCurrentLanguage();
            int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.NEWS_ID]);

            News objNews = new News();
            objNews.LoadById(nItemID);
            if (nItemID == 0)
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                switch (nCurrentLang)
                {
                    case 1:
                        lblTitle.Text = objNews.Data.NewsTitle1;
                        ltrContent.Text = objNews.Data.MainContent1;
                        lblInitData.Text = objNews.Data.InitContent1;
                        break;
                    case 2:
                        lblTitle.Text = objNews.Data.NewsTitle2;
                        ltrContent.Text = objNews.Data.MainContent2;
                        lblInitData.Text = objNews.Data.InitContent2;
                        break;
                    case 3:
                        lblTitle.Text = objNews.Data.NewsTitle3;
                        ltrContent.Text = objNews.Data.MainContent3;
                        lblInitData.Text = objNews.Data.InitContent3;
                        break;
                }

                lblDatePosted.Text = MultipleLanguage.POSTED_DATE[nCurrentLang - 1] + ConvertData.ConvertIntToDatetimeString(objNews.Data.CreatedDate);
                lblSource.Text = objNews.Data.Source;

                NewsComment objNewsComment = new NewsComment();
                DataTable dtbNewsComment = new DataTable();
                dtbNewsComment = objNewsComment.Search(nItenView, sTitle, nItemID, 1, 1, 10, ref nPageCount);
                if (dtbNewsComment.Rows.Count > 0)
                {
                    dtlComment.DataSource = dtbNewsComment;
                    dtlComment.DataBind();
                    divComment.Visible = true;
                    lblComment.Text = "Bình luận cho bài viết này" + " " + "(" + dtbNewsComment.Rows.Count + " " + "bình luận" + ")";
                }
                else
                {
                    divComment.Visible = false;
                }
                lblContent.Text = MultipleLanguage.CONTENT[nCurrentLang - 1];
                lblUsername.Text = MultipleLanguage.FULLNAME[nCurrentLang - 1];
                lblEmail.Text = MultipleLanguage.EMAIL[nCurrentLang - 1];
                lblCommentInfo.Text = MultipleLanguage.COMMENT_INFO[nCurrentLang - 1];
                btnSave.Text = MultipleLanguage.BTN_SAVE[nCurrentLang - 1];
                rfvUsername.ErrorMessage = MultipleLanguage.USERNAME_EMPTY[nCurrentLang - 1];
                rfvEmail.ErrorMessage = MultipleLanguage.EMAIL_EMPTY[nCurrentLang - 1];
                revEmail.ErrorMessage = MultipleLanguage.EMAIL_WRONG[nCurrentLang - 1];
                rfvContent.ErrorMessage = MultipleLanguage.CONTENT_EMPTY[nCurrentLang - 1];
                lbNote.Text = MultipleLanguage.COMMENT_NOTE[nCurrentLang - 1];
            }
        }
        catch { Response.Redirect("~/Default.aspx"); }
    }

    protected void dtlComment_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        int nCurrentLanguage = Support.GetCurrentLanguage();
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblCommentDetail = (Label)e.Item.FindControl("lblCommentDetail");
            Label lblEmail = (Label)e.Item.FindControl("lblEmail");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CommentID"));
            int nNewsID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID"));

            lblCommentDetail.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Content"));
            int nPostedDate = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Dateposted"));
            string sEmail = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Email"));
            lblEmail.Text = ConvertData.ConvertIntToDatetimeString(nPostedDate) + "," + " " + MultipleLanguage.BY[nCurrentLanguage - 1] + " " + sEmail;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nCurrentLanguage = Support.GetCurrentLanguage();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.NEWS_ID]);
        NewsComment objNewsComment = new NewsComment();
        objNewsComment.Data.Content = ConvertData.ConvertToString(txtContent.Text);
        objNewsComment.Data.Dateposted = ConvertData.ConvertDatetimeToInt(DateTime.Today);
        objNewsComment.Data.NewsID = nItemID;
        objNewsComment.Data.Status = EnumeType.INACTIVE;
        objNewsComment.Data.Email = ConvertData.ConvertToString(txtEmail.Text);
        int nResult = objNewsComment.Insert();
        if (nResult > 0)
        {
            Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nCurrentLanguage - 1]);
            txtContent.Text = string.Empty;
        }
        else
        {
            Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nCurrentLanguage - 1]);
        }
    }
}